package com.maigao.api.controller;

import com.maigao.api.interceptors.identify.Identify;
import com.maigao.entity.ResultJson;
import com.maigao.entity.form.AddClassRecordForm;
import com.maigao.entity.vo.ClassRecordDetailVO;
import com.maigao.entity.vo.ClassRecordVO;
import com.maigao.entity.vo.CoachClassRecordSummaryVO;
import com.maigao.service.ClassRecordService;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2024-10-23 22:54
 **/
@RestController
@RequestMapping(value = "/classRecord")
public class ClassRecordController {

    @Autowired
    private ClassRecordService classRecordService;

    @Identify
    @ResponseBody
    @RequestMapping("add")
    public ResultJson addClassRecord(AddClassRecordForm addClassRecordForm) {
        Pair<Boolean, String> addResult = classRecordService.addClassRecord(addClassRecordForm);
        if (addResult.getLeft()) {
            return ResultJson.success("");
        }
        return ResultJson.fail(addResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("revoke")
    public ResultJson revokeClassRecord(@RequestParam Integer recordId) {
        Pair<Boolean, String> addResult = classRecordService.revokeClassRecord(recordId);
        if (addResult.getLeft()) {
            return ResultJson.success("");
        }
        return ResultJson.fail(addResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("finish")
    public ResultJson finishClassRecord(@RequestParam List<Integer> recordIds) {
        Pair<Boolean, String> addResult = classRecordService.finishClassRecords(recordIds);
        if (addResult.getLeft()) {
            return ResultJson.success("");
        }
        return ResultJson.fail(addResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("findWeekClassRecordByCoach")
    public ResultJson findWeekClassRecord(@RequestParam Integer coachId, @RequestParam Integer week) {
        Triple<Boolean, String, List<ClassRecordDetailVO>> findResult = classRecordService.findWeekClassRecordByCoachId(coachId, week);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return ResultJson.fail(findResult.getMiddle());
    }

    @Identify
    @ResponseBody
    @RequestMapping("findClassRecordByStudent")
    public ResultJson findClassRecordByStudent(@RequestParam Integer studentId, @RequestParam String fromTime, @RequestParam String endTime) {
        Triple<Boolean, String, List<ClassRecordVO>> findResult = classRecordService.findStudentClassRecords(Lists.newArrayList(studentId), fromTime, endTime);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return ResultJson.fail(findResult.getMiddle());
    }

    @Identify
    @ResponseBody
    @RequestMapping("coachClassRecordSummary")
    public ResultJson findCoachClassRecordSummary(@RequestParam String fromTime, @RequestParam String endTime) {
        Triple<Boolean, String, CoachClassRecordSummaryVO> findResult = classRecordService.findCoachClassRecordSummary(fromTime, endTime);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return ResultJson.fail(findResult.getMiddle());

    }

}
